package com.zzx.boot.servlet.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.entity.table.Feedback;
import com.zzx.boot.entity.vo.FeedbackVo;
import com.zzx.boot.entity.vo.UserVo;
import com.zzx.boot.mapper.FeedbackMapper;
import com.zzx.boot.servlet.FeedbackService;
import com.zzx.boot.servlet.UserService;
import com.zzx.boot.utils.DateTImeUtils;
import com.zzx.boot.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/11 14:39
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Resource
    private UserService userService;

    @Override
    public List<FeedbackVo> getFeedback(Long toId,Long current) {
        Page<Feedback> page = new Page<>();
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Feedback::getToId,toId);
        baseMapper.selectPage(page,wrapper);
        List<FeedbackVo> vos = page.getRecords().stream().map(item -> {
            UserVo userVo = userService.getUserById(item.getFromId());
            FeedbackVo vo = new FeedbackVo(item.getId().toString(), userVo, item.getData(), item.getCreateTime());
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public String addFeedback(Long toId,String data) {
        int i = baseMapper.insert(new Feedback(null, toId, SecurityUtils.getUserId(), data, DateTImeUtils.getDate()));
        return i==1?"反馈成功":"反馈失败";
    }
}
